namespace Domotica.Media
{
    using Microsoft.VisualBasic;
    using System;
    using System.Collections.Generic;
    using WMPLib;

    public class Track : IComparable<Track>
    {
        private Domotica.Media.Album _album;
        private string _location;
        private IWMPMedia _MediaItem;
        private string _name;
        private int? _number;
        private bool _NumberSet;
        private string _UniqueFileIdentifier;

        public Track(IWMPMedia mediaItem, Domotica.Media.Album album)
        {
            this._MediaItem = mediaItem;
            this._album = album;
        }

        public int CompareTo(Track other)
        {
            return Comparer<int?>.Default.Compare(this.Number, other.Number);
        }

        public override string ToString()
        {
            return this.Name;
        }

        public Domotica.Media.Album Album
        {
            get
            {
                return this._album;
            }
        }

        public int Index
        {
            get
            {
                return this._album.Tracks.IndexOf(this);
            }
        }

        public string Location
        {
            get
            {
                if (string.IsNullOrEmpty(this._location))
                {
                    this._location = this._MediaItem.getItemInfo("SourceUrl");
                }
                return this._location;
            }
        }

        public IWMPMedia MediaItem
        {
            get
            {
                return this._MediaItem;
            }
        }

        public string Name
        {
            get
            {
                if (string.IsNullOrEmpty(this._name))
                {
                    this._name = this._MediaItem.getItemInfo("Title");
                }
                return this._name;
            }
        }

        public int? Number
        {
            get
            {
                if (!this._NumberSet)
                {
                    this._NumberSet = true;
                    int _trackNumber = 0;
                    if (int.TryParse(this._MediaItem.getItemInfo("OriginalIndex"), out _trackNumber))
                    {
                        this._number = _trackNumber;
                    }
                }
                return this._number;
            }
        }

        public string UniqueFileIdentifier
        {
            get
            {
                if (this._UniqueFileIdentifier == null)
                {
                    this._UniqueFileIdentifier = this._MediaItem.getItemInfo("UniqueFileIdentifier");
                    if (string.IsNullOrEmpty(this._UniqueFileIdentifier))
                    {
                        this._UniqueFileIdentifier = Guid.NewGuid().ToString();
                        this._MediaItem.setItemInfo("UniqueFileIdentifier", this._UniqueFileIdentifier);
                    }
                }
                return this._UniqueFileIdentifier;
            }
        }
    }
}

